package com.lovo.dao.impl;

import com.lovo.dao.BaseDAO;
import com.lovo.dao.UserDAO;
import com.lovo.pojo.UesrDO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDAOImpl extends BaseDAO implements UserDAO {

    @Override
    public UesrDO getByUserNameAndPWd(String username, String pwd) {
         this.getConnection();
         statement =null;
         rs =null;
        UesrDO uesrs =null;
        String str = "select * from t_atm where a_account=? and a_password=?";
        try {
            statement=con.prepareStatement(str);
            statement.setObject(1,username);
            statement.setObject(2,pwd);
            rs = statement.executeQuery();
            if(rs.next()){
                uesrs =new UesrDO();
                uesrs.setAccount(rs.getString("a_account"));
                uesrs.setPassword(rs.getString("a_password"));
                uesrs.setBalance(rs.getInt("a_balance"));
                uesrs.setId(rs.getInt("a_id"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            close(con,statement,rs);
        }
        return uesrs;
    }

    @Override
    public void update(UesrDO uesr) {

//        PreparedStatement sta = null;
        this.getConnection();
        statement =null;
//         获取sql语句对象
        String str = "update t_atm set a_password=?,a_balance=?  WHERE a_id=?";
        try {
            statement = con.prepareStatement(str);
            statement.setObject(1, uesr.getPassword());
            statement.setObject(2, uesr.getBalance());
            statement.setObject(3, uesr.getId());
            //执行SQL语句
            int row = statement.executeUpdate();
            System.out.println("影响行数" + row + "行");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭连接
            close(con, statement);
        }

    }

    @Override
    public UesrDO getById(int id) {
       this.getConnection();
         statement = null;
         rs = null;
        UesrDO uesr = null;
        String str = "select * from t_atm where a_id=?";
        try {
            statement = con.prepareStatement(str);
            statement.setObject(1, id);

            rs = statement.executeQuery();
            if (rs.next()) {
                uesr = new UesrDO();
                uesr.setAccount(rs.getString("a_account"));
                uesr.setPassword(rs.getString("a_password"));
                uesr.setBalance(rs.getInt("a_balance"));
                uesr.setId(rs.getInt("a_id"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con,statement,rs);
        }
        return uesr;
    }

    /*
    * 通过姓名查询
    * */
    @Override
    public UesrDO getByName(String name) {
        this.getConnection();
         statement = null;
         rs = null;
        UesrDO uesrDO = null;
        String str = "select * from t_atm where a_account=?";
        try {
            statement = con.prepareStatement(str);
            statement.setObject(1, name);

            rs = statement.executeQuery();
            if (rs.next()) {
                uesrDO = new UesrDO();
                uesrDO.setAccount(rs.getString("a_account"));
                uesrDO.setPassword(rs.getString("a_password"));
                uesrDO.setBalance(rs.getInt("a_balance"));
                uesrDO.setId(rs.getInt("a_id"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, statement, rs);
        }
        return uesrDO;
    }


}
